/** Copyright 2014, Ross Mahony. Some rights reserved. See COPYING. */
package models

import play.api.db.slick.Config.driver.simple._
import scala.slick.lifted.Tag

case class Property(id: Option[Long], postCode: String, tenure: Int, 
  age: String, propertyType: Int, bedrooms: Int)

/** Property entity. */
class PropertyTbl(tag: Tag) extends Table[Property](tag, "PROP") {

  def id = column[Long]("ID", O.PrimaryKey, O.AutoInc)
  def postCode = column[String]("POSTCODE", O.NotNull)
  def tenure = column[Int]("TENURE", O.NotNull)
  def age = column[String]("AGE", O.NotNull)
  def propertyType = column[Int]("TYP", O.NotNull)
  def bedrooms = column[Int]("ROOMS", O.NotNull)

  def * = (id.?, postCode, tenure, age, propertyType, bedrooms) <> (Property.tupled, Property.unapply _)
}

/** Property DAO */
object PropertyDAO {
  val properties = TableQuery[PropertyTbl]
  
 /**
  * Retrieve a [[models.Property]] by the id
  *
  * @param id
  */
  def findById(id: Long)(implicit s: Session): Option[Property] =
  properties.where(_.id === id).firstOption


 /** Count all properties */
  def count(implicit s: Session): Int =
  Query(properties.length).first

 /**
  * Insert a new [[models.Property]]
  *
  * @param property
  */
  def insert(property: Property)(implicit s: Session) {
    properties.insert(property)
  }

 /**
  * Insert a new [[models.Property]] by ignoring auto inc column
  *
  * @param property
  */
  def forceInsert(property: Property)(implicit s: Session) {
    properties.forceInsert(property)
  }

 /** Delete all properties */
  def deleteAll(implicit s: Session) {
    properties.delete
  }

}
